/*
 ============================================================================
 Name		: DiskBrowser.cpp
 Author	  : Lenka Litvova
 Version	 : 1.0
 Copyright   : Copyright belongs to Lenka Litvova
 Description : CDiskBrowser implementation
 ============================================================================
 */

#include "DiskBrowser.h"
#include <bautils.h> //for BaflUtils, link against: bafl.lib

CDiskBrowser::CDiskBrowser()
	{
	// No implementation required
	}

CDiskBrowser::~CDiskBrowser()
	{
		delete iFileList;
		delete iDirList;
		iFileServer.Close();
	}

CDiskBrowser* CDiskBrowser::NewLC()
	{
	CDiskBrowser* self = new (ELeave) CDiskBrowser();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CDiskBrowser* CDiskBrowser::NewL()
	{
	CDiskBrowser* self = CDiskBrowser::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CDiskBrowser::ConstructL()
	{
		User::LeaveIfError(iFileServer.Connect());
	}

TInt CDiskBrowser::GetDirectoryAndFileList(const TDesC& aPath)
	{
	if (iFileList)
		{
		delete iFileList;
		iFileList = NULL;
		}
	if (iDirList)
		{
		delete iDirList;
		iDirList = NULL;
		}
	
	TInt result = iFileServer.GetDir(aPath,
                             KEntryAttNormal,
                             ESortByName | EDirsFirst | EAscending,
                             iFileList,
                             iDirList);
	return (result);
	}
	
TInt CDiskBrowser::DirectoryCount()
	{
	if (iDirList)
		return (iDirList->Count());
	else
		return (0);
	}
	
	
TInt CDiskBrowser::FileCount()
	{
	if (iFileList)
		return (iFileList->Count());
	else
		return (0);
	}
	
	
const TDesC& CDiskBrowser::DirectoryName(TInt aDirListIndex)
	{
	if ( (!iDirList) || (iDirList->Count()<=aDirListIndex) )
		return (KNullDesC);
	
	else
		return (iDirList->operator[](aDirListIndex).iName);
	
	}

const TDesC& CDiskBrowser::FileName(TInt aFileNameIndex)
	{
	if ( (!iFileList) || (iFileList->Count()<=aFileNameIndex) )
		return (KNullDesC);
	
	else
		return (iFileList->operator[](aFileNameIndex).iName);
	}

TBool CDiskBrowser::IsDirectory(const TDesC& aPath)
	{
		TBool result = EFalse;
		BaflUtils::IsFolder(iFileServer, aPath, result);
		return result;
	}
